package top.topsmart.api.bycenter.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.topsmart.api.bycenter.entity.view.CommitUser;
import top.topsmart.api.bycenter.mapper.DynamicMapper;
import top.topsmart.api.bycenter.mapper.InfoMapper;
import top.topsmart.api.bycenter.mapper.UserMapper;
import top.topsmart.api.bycenter.mapper.UserOperationMapper;
import top.topsmart.api.bycenter.model.Dynamic;
import top.topsmart.api.bycenter.model.Info;
import top.topsmart.api.bycenter.model.User;
import top.topsmart.api.bycenter.model.UserOperation;
import top.topsmart.api.bycenter.service.UserOperationService;
import top.topsmart.api.bycenter.util.Result;
import top.topsmart.api.bycenter.util.TimeHandler;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserOperationServiceImpl implements UserOperationService {
    @Autowired
    private UserOperationMapper userOperationMapper;
    @Autowired
    private InfoMapper infoMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DynamicMapper dynamicMapper;

    @Override
    public Result operate(JSONObject operation) {
        //接受参数
        Integer type = operation.getInteger("type");
        String handler = operation.getString("handler");
        String object = operation.getString("object");
//        String parent = operation.getString("parent");
        String content = operation.getString("content");


        if (type == null || handler == null || object == null) {
            return new Result(1524, "type,handler,object不能为空");
        } else {
            //点赞
            if (type == 1) {
                UserOperation tradition = new UserOperation();
                tradition.setType(1);
                tradition.setHandler(handler);
                tradition.setObject(object);
                UserOperation res = userOperationMapper.selectSelective(tradition);
                //操作过
                if (res != null) {
                    UserOperation operation1 = new UserOperation();
                    operation1.setId(res.getId());
                    operation1.setLikeStatus(!res.getLikeStatus());

                    userOperationMapper.updateByPrimaryKeySelective(operation1);
                    return new Result(0, "success");
                }
                //未操作过
                else {
                    UserOperation operation1 = new UserOperation();
                    operation1.setId(UUID.randomUUID().toString());
                    operation1.setType(1);
                    operation1.setHandler(handler);
                    operation1.setObject(object);
                    operation1.setLikeStatus(true);
                    operation1.setDoTime(new Date());
                    operation1.setDelFlag(false);

                    userOperationMapper.insertSelective(operation1);
                    return new Result(0, "success");
                }
            }
            //关注
            else if (type == 2) {
                UserOperation tradition = new UserOperation();
                tradition.setType(2);
                tradition.setHandler(handler);
                tradition.setObject(object);
                UserOperation res = userOperationMapper.selectSelective(tradition);

                //操作过
                if (res != null) {
                    UserOperation operation1 = new UserOperation();
                    operation1.setId(res.getId());
                    operation1.setFollowStatus(!res.getFollowStatus());

                    userOperationMapper.updateByPrimaryKeySelective(operation1);
                    return new Result(0, "success");
                }
                //未操作过
                else {
                    UserOperation operation1 = new UserOperation();
                    operation1.setId(UUID.randomUUID().toString());
                    operation1.setType(2);
                    operation1.setHandler(handler);
                    operation1.setObject(object);
                    operation1.setFollowStatus(true);
                    operation1.setDoTime(new Date());
                    operation1.setDelFlag(false);

                    userOperationMapper.insertSelective(operation1);
                    return new Result(0, "success");
                }
            }
            //评论
            else {
                UserOperation operation1 = new UserOperation();
                operation1.setId(UUID.randomUUID().toString());
                operation1.setType(3);
                operation1.setHandler(handler);
                operation1.setObject(object);
                operation1.setContent(content);
                operation1.setDoTime(new Date());
                operation1.setDelFlag(false);
                userOperationMapper.insertSelective(operation1);
                return new Result(0, "success");
            }
        }


    }

    /**
     * 参数：dynamicId userId
     *
     * @param param
     * @return
     */
    @Override
    public Result getDynamicDetail(JSONObject param) {

        //接受请求参数
        String dynamicId = param.getString("dynamicId");
        String userId = param.getString("userId");

        //查询数据库中的动态和用户是否存在
        Dynamic dynamic = dynamicMapper.selectByPrimaryKey(dynamicId);
        User user = userMapper.selectByPrimaryKey(userId);

        //参数异常处理
        if (dynamicId == null || userId == null) {
            return new Result(1531, "dynamicId 和 userId 为必须参数");
        } else if (dynamic == null) {
            return new Result(1532, "动态不存在");
        } else if (user == null) {
            return new Result(1533, "用户不存在");
        } else {

            //创建响应结果接受容器
            JSONObject data = new JSONObject();
            Boolean followStatus;
            List<String> lovers = new ArrayList<String>();
            List<CommitUser> commitUsers = new ArrayList<CommitUser>();


            //关注状态
            UserOperation tradition = new UserOperation();
            tradition.setHandler(userId);
            tradition.setObject(dynamic.getPublisher());

            UserOperation userOperation = userOperationMapper.getFollowStatus(tradition);
            if (userOperation == null) {
                data.put("followStatus", false);
            } else {
                data.put("followStatus", userOperation.getFollowStatus());
            }

            //获取点赞的人的头像
            UserOperation SParam_dynamic =  new UserOperation();
            SParam_dynamic.setObject(dynamicId);
            List<UserOperation> userOperations = userOperationMapper.getLoves(SParam_dynamic);
            if (userOperations.size() == 0) {
                data.put("loves", lovers);
            } else {
                for (int i = 0; i < userOperations.size(); i++) {
                    UserOperation userOperation1 = userOperations.get(i);
                    User user1 = userMapper.selectByPrimaryKey(userOperation1.getHandler());
                    Info info = infoMapper.selectByPrimaryKey(user.getInfoId());
                    lovers.add(info.getAvatar());
                }
                data.put("loves", lovers);
            }

            //获取评论
            UserOperation SParam_commits = new UserOperation();
            SParam_commits.setObject(dynamicId);
            List<UserOperation> userOperationList = userOperationMapper.getCommits(SParam_commits);
            if (userOperationList.size() == 0) {
                data.put("commits", commitUsers);
            } else {
                for (int i = 0; i < userOperationList.size(); i++) {
                    UserOperation userOperation1 = userOperationList.get(i);
                    CommitUser commitUser = new CommitUser();

                    User commiter = userMapper.selectByPrimaryKey(userOperation1.getHandler());
                    Info info = infoMapper.selectByPrimaryKey(commiter.getInfoId());
                    UserOperation tr = new UserOperation();
                    tr.setHandler(userId);
                    tr.setObject(commiter.getId());
                    UserOperation follow_status = userOperationMapper.getFollowStatus(tr);
                    Boolean follow;
                    if (follow_status==null){
                        follow = false;
                    } else {
                        follow = follow_status.getFollowStatus();
                    }

                    commitUser.setAvatar(info.getAvatar());
                    commitUser.setNickname(info.getNickname());
                    commitUser.setLevel(commiter.getLevel());
                    commitUser.setContent(userOperation1.getContent());
                    commitUser.setCommitTime(new TimeHandler().TimeHandler(userOperation1.getDoTime()));
                    commitUser.setFollowStatus(follow);

                    commitUsers.add(commitUser);
                }
                data.put("commits", commitUsers);
            }
            return new Result(0, "success", data);
        }
    }

    public Result update(JSONObject param){
        if (param.getString("id") == null) {
            return new Result(1535, "id不能为空");
        } else {
            UserOperation userOperation = new UserOperation();

            userOperation.setId(param.getString("id"));
            userOperation.setDelFlag(param.getBoolean("delFlag")); //修改发布范围

            userOperationMapper.updateByPrimaryKeySelective(userOperation);
            return new Result(0, "success");
        }

    }
}
